package com.yf.dxapp.module.app.controller.admin.token;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.token.vo.TokenPageReqVO;
import com.yf.dxapp.module.app.controller.admin.token.vo.TokenRespVO;
import com.yf.dxapp.module.app.controller.admin.token.vo.TokenSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.token.TokenDO;
import com.yf.dxapp.module.app.service.token.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 登录token")
@RestController
@RequestMapping("/dxapp/oken")
@Validated
public class TokenController {

    @Resource
    private TokenService okenService;

    @PostMapping("/create")
    @Operation(summary = "创建登录token")
    @PreAuthorize("@ss.hasPermission('t:oken:create')")
    public CommonResult<Long> createoken(@Valid @RequestBody TokenSaveReqVO createReqVO) {
        return success(okenService.createoken(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新登录token")
    @PreAuthorize("@ss.hasPermission('t:oken:update')")
    public CommonResult<Boolean> updateoken(@Valid @RequestBody TokenSaveReqVO updateReqVO) {
        okenService.updateoken(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除登录token")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:oken:delete')")
    public CommonResult<Boolean> deleteoken(@RequestParam("id") Long id) {
        okenService.deleteoken(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得登录token")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:oken:query')")
    public CommonResult<TokenRespVO> getoken(@RequestParam("id") Long id) {
        TokenDO oken = okenService.getoken(id);
        return success(BeanUtils.toBean(oken, TokenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得登录token分页")
    @PreAuthorize("@ss.hasPermission('t:oken:query')")
    public CommonResult<PageResult<TokenRespVO>> getokenPage(@Valid TokenPageReqVO pageReqVO) {
        PageResult<TokenDO> pageResult = okenService.getokenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TokenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出登录token Excel")
    @PreAuthorize("@ss.hasPermission('t:oken:export')")
    
    public void exportokenExcel(@Valid TokenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TokenDO> list = okenService.getokenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "登录token.xls", "数据", TokenRespVO.class,
                        BeanUtils.toBean(list, TokenRespVO.class));
    }

}